﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace Alimentador.Foods
{
    /// <summary>
    /// Constructor
    /// </summary>
    [Serializable]
    public class Food : FoodBase
    {
        private List<FoodProperty> m_propertyList;

        [XmlElement("PropertyList")]
        public List<FoodProperty> PropertyList
        {
            get { return m_propertyList; }
            set { m_propertyList = value; }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public Food()
        {
            this.GetName = "";
            m_propertyList = new List<FoodProperty>();
        }

        private double m_quantity = 0;

        public double Quantity
        {
            get { return Math.Round(m_quantity, 2); }
            set { GetQuantity = value.ToString(); }
        }

        public string GetQuantity
        {
            get
            {
                FoodProperty fp = FindPropertyByName(PropertyName.Porcao.ToString());
                if (fp != null)
                {
                    return fp.Quantity.ToString("F0");
                }
                return "";
            }
            set
            {
                Double.TryParse(value, out m_quantity);

                FoodProperty fp = FindPropertyByName(PropertyName.Porcao.ToString());
                if (fp != null)
                {
                    fp.Quantity = Math.Round(m_quantity, 2);
                }
            }
        }


        public string GetUnit
        {
            get
            {
                FoodProperty fp = FindPropertyByName(PropertyName.Porcao.ToString());
                if (fp != null)
                {
                    return fp.Unit;
                }
                return "";
            }
            set
            {
                FoodProperty fp = FindPropertyByName(PropertyName.Porcao.ToString());
                if (fp != null)
                {
                    fp.Unit = value;
                }
            }
        }

        /// <summary>
        /// Find the property or more for the given name value. If not found returns an empty list
        /// </summary>
        /// <param name="propertyName">string</param>
        /// <returns>List(FoodProperty)</returns>
        public FoodProperty FindPropertyByName(string propertyName)
        {
            try
            {
                FoodProperty fp = m_propertyList.Find(f => f.PropertyName.ToString().Contains(propertyName));
                return fp;
            }
            catch (Exception)
            {
                return new FoodProperty();
            }
        }

        /// <summary>
        /// Removes a food property according to given name
        /// </summary>
        /// <param name="propertyName">string</param>
        /// <returns>bool</returns>
        public bool RemovePropertyByName(string propertyName)
        {
            try
            {
                FoodProperty foodProp = new FoodProperty();
                foodProp = m_propertyList.Find(f => f.PropertyName.ToString().Contains(propertyName));
                return m_propertyList.Remove(foodProp);
            }
            catch
            {
                return false;
            }
        }
    }
}
